import * as cheerio from 'cheerio';
import fs from 'fs';
import path from 'path';
import he from 'he';
import { isHtmlFile } from './util.js'
import htmlMinifier from 'html-minifier'
const outputPath = path.join(process.cwd(), 'Documentation');

// 处理翻译后的html内容转义问题
function escapeHtml() {
    try {
        const files = fs.readdirSync(outputPath);
        const contents = [];
        files.forEach(file => {
            const filePath = path.join(outputPath, file);
            const stats = fs.statSync(filePath);
            if (stats.isFile() && isHtmlFile(file)) {
                console.log('正在处理转义文件：', file)
                const content = fs.readFileSync(filePath, 'utf-8');
                const resultHtml = he.decode(content);
                try {
                    const miniResult = htmlMinifier.minify(resultHtml, {
                        collapseWhitespace: true, // 压缩HTML
                        removeComments: true, // 移除HTML注释
                        minifyCSS: false, // 压缩CSS
                        minifyJS: false // 压缩JS
                    })
                    const outputFilePath = path.join(outputPath, file);
                    fs.writeFileSync(outputFilePath, miniResult);

                } catch (err) {
                    console.log('文件：', file, '转义失败')
                }

            }
        });
        return contents;
    } catch (err) {
        console.error('Error reading Documentation folder:', err);
        return [];
    }
}
function main() {
    escapeHtml()
}

main()